import { _decorator, Component, Node, SystemEvent, Touch, EventTouch, input, Input } from 'cc';
import { GameManager } from '../framework/GameManager';
const { ccclass, property } = _decorator;

@ccclass('UIMain')
export class UIMain extends Component {
    // 玩家飞机面板
    @property(Node)
    public playerPlane: Node = null;
    // 玩家飞机面板移动速度
    @property
    public planeSpeed = 5;
    // 游戏管理器
    @property(GameManager)
    public gameManager: GameManager = null;
    @property(Node)
    public gameStart: Node = null;
    @property(Node)
    public game: Node = null;
    @property(Node)
    public gameOver: Node = null;

    start() {
        this.node.on(SystemEvent.EventType.TOUCH_START, this._touchStart, this);//监听触摸开始
        this.node.on(SystemEvent.EventType.TOUCH_MOVE, this._touchMove, this);//监听触摸
        this.node.on(SystemEvent.EventType.TOUCH_END, this._touchEnd, this);//监听触摸结束
        this.gameStart.active = true;
    }

    // 再来一局
    public reStart() {
        this.gameOver.active = false;
        this.game.active = true;
        this.gameManager.playAudioEffect('button');
        this.gameManager.gameReStart();
    }

    // 返回主界面
    public returnMain() {
        this.gameOver.active = false;
        this.gameStart.active = true;
        this.gameManager.playAudioEffect('button');
        this.gameManager.returnMain();
    }

    //触摸开始事件处理->标记游戏管理器变量,射击开始
    _touchStart(touch: Touch, event: EventTouch) {
        if (this.gameManager.isGameStart) {
            this.gameManager.isShooting(true);
        } else {
            this.gameStart.active = false;
            this.game.active = true;
            this.gameManager.playAudioEffect('button');
            this.gameManager.gameStart();
        }
    }

    //触摸事件处理->更新飞机的位置
    _touchMove(touch: Touch, event: EventTouch) {
        if (!this.gameManager.isGameStart) {
            return;
        }
        const delta = touch.getDelta();
        let pos = this.playerPlane.position;
        this.playerPlane.setPosition(pos.x + 0.01 *this.planeSpeed * delta.x, pos.y, pos.z - 0.01 * this.planeSpeed * delta.y);
    }

    // 触摸结束事件处理->标记游戏管理器变量,射击结束
    _touchEnd(touch: Touch, event: EventTouch) {
        if (!this.gameManager.isGameStart) {
            return;
        }
        this.gameManager.isShooting(false);
    }
}


